//
//  StudyViewController.swift
//  weibo
//
//  Created by tomato on 2021/9/27.
//

import UIKit
import Alamofire

class StudyViewController: UIViewController {

    // MARK: - 基础语法学习
    //let 声明常量  var 声明变量 结束语句可以不加分号
    let a:Int = 20
    var b:Double = 30.89
    let c:Double = 8.09
    let redView:UIView = UIView()
//    var name  = "刘德华"
    //字典
    let personDic = ["name":"刘德华","age":"30"]//不可变
    var dicM = [String:AnyObject]()//可变
    //可选类型
//  var name:String = nil
//  这里不能赋值nil,因为在swift中，nil也是一个类型，类型不一致，不能直接赋值
    //但是在创建对象的时候，都必须有明确的初始化值
//    var name:Optional<String> = nil 不常用
    var name : String? = nil//常用写法
    var callBack:((_ jsonResult:String)->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
//        wangluoqingqiu_Alamofir()
        wangluoqingqiu_af()
    }
}

// MARK: - Swift 语法学习
extension  StudyViewController{
       
    func yuFaStudy() -> Void {
        //基础语法学习
        //元组
        //可选类型
        //? !强制解包
        //判断
        //打印语句
//        print("学习")
//        print("🍎🍎🍎🍎🍎我是首页")
//        print(name)
        zidian()
        yuanzhu()
        kexuan()
        bibao()
    }

    func method1(){
//        print("🍎🍎🍎🍎🍎无参数无返还1")
    }
    func method2() -> Void {
//        print("🍎🍎🍎🍎🍎无参数无返还2")
    }
    func method3(canshu1:String) -> String {
        return canshu1;
    }
    
    func zidian() {
        //不可变
//        print(personDic)
        //可变增加
        dicM["name"] = "星爷" as AnyObject
        dicM["age"] = 20 as AnyObject
//        print(dicM)
        //删
        dicM.removeValue(forKey:"age")
//        print(dicM)
        //改
        dicM["name"] = "唐伯虎" as AnyObject
//        print(dicM)
        //查
        let name = dicM["name"]!
        print(name)
        //遍历字典的值
        for key in personDic.keys{
            print(key)
        }
        for (key,value) in personDic{
            print(key)
            print(value)
        }
    }
    
    func yuanzhu() {
        //给每一个元素起别名,作为方法的返回值
        let info = (name:"啊哈哈",age:88)
        print(info.name)
    }
    
    func kexuan() {
//      给可选值赋值
      name = "刘德华"
//      取出可选类型的值：可选类型 + ！:强制解包
//        强制解包是一个危险的操作，会导致程序崩溃
      print(name!)
//     可选绑定
        if let name = name{
            //如果不为nil,就打印
            print(name)
        }
    }
    //闭包
    func bibao() {
//    闭包类型:(参数列表)->(返回值)
      //weak
        weak var weakSelf = self
        weakSelf?.view.backgroundColor = UIColor.red
    }

    //Alamofire网络请求
    func wangluoqingqiu_Alamofir(){
        
        let params = ["name":"华仔","age":"18"]
        Alamofire.request("http://httpbin.org/get",parameters: params).responseJSON { response in
            guard let result = response.result.value else{
//                Print(items: response.result.error)
                return
            }
            print(result)
        }
        
    }
    
    func wangluoqingqiu_af(){
        let params = ["name":"liu de hua ","age":"18"]
        NetworkAFTools.sharedInstance.request(method: .GET, urlString: "http://httpbin.org/get", parameters:params) { reslut, error in
            if(error != nil){
                Print(items: error)
                return
            }
            Print(items: reslut)
            
        }
    }
}
